﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Core
{
    public class ValidationMessageCollection : IValidationMessageCollection
    {
        private IList<ValidationMessage> _messages;

        public ValidationMessageCollection()
        {
            this._messages = new List<ValidationMessage>();
        }

        public void AddErrorMessage(string message)
        {
            this.AddMessage(message, ValidationMessageSeverity.Error);
        }

        public void AddWarningMessage(string message)
        {
            this.AddMessage(message, ValidationMessageSeverity.Warning);
        }

        public void AddMessage(ValidationMessage message)
        {
            message.ThrowIfNull("message");
            this._messages.Add(message);
        }

        public void Clear()
        {
            this._messages.Clear();
        }

        public bool IsValid()
        {
            return this.IsValid(false);
        }

        public bool IsValid(bool includeWarnings)
        {
            return includeWarnings
                ? this._messages.Count == 0
                : this.ErrorCount == 0;
        }

        private void AddMessage(string message, ValidationMessageSeverity severity)
        {
            this._messages.Add(new ValidationMessage(message, severity));
        }

        public IEnumerable<ValidationMessage> ErrorMessages
        {
            get { return this._messages.Where(m => m.Severity == ValidationMessageSeverity.Error); }
        }

        public IEnumerable<ValidationMessage> WarningMessages
        {
            get { return this._messages.Where(m => m.Severity == ValidationMessageSeverity.Warning); }
        }

        public IEnumerable<ValidationMessage> AllMessages
        {
            get { return this._messages; }
        }

        public int ErrorCount
        {
            get { return this.ErrorMessages.Count(); }
        }

        public int WarningCount
        {
            get { return this.WarningMessages.Count(); }
        }
    }
}
